<template>
  <div class='search'>
    <f-form ref='form' :action='api.payment' :form-key='this.formItem.paymentId' :file-ids='fileIds' handler-name='hyYfkPaymentSaveHandler'>
      <f-form-item label='单据类型' type='radio' name='amountType' :required='true' :data='api.busCommon.amountType' :value.sync='formItem.amountType' />
      <f-form-item label='收款金额' placeholder='请选择收款金额' name='amount' disabled :required='true' :value.sync='formItem.amount' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择银行流水'>
          <a-icon type='money-collect' style='cursor:pointer' @click='openBankStatementModal' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='收款日期' type='date' disabled name='amountDate' required :value='formItem.amountDate' />
      <f-form-item label='收款账户' placeholder='请选择收款账户' name='bankName' disabled :readOnly='true' :required='true' :value.sync='formItem.bankName' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择'>
          <a-icon type='money-collect' style='cursor:pointer' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='单位名称' placeholder='请选择单位' name='customName' @click='openCustomModal' :readOnly='true' :required='true' :value.sync='formItem.customName' :trigger-change='true' v-if='formItem.amountType!="CustomBond"'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择单位'>
          <a-icon type='team' style='cursor:pointer' @click='openCustomModal' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='部门' placeholder='请选择部门' name='deptName' @click='openDeptModal' :readOnly='true' :required='true' :value.sync='formItem.deptName' :trigger-change='true'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择部门'>
          <a-icon type='team' style='cursor:pointer' @click='openDeptModal' />
        </a-tooltip>
      </f-form-item>
      <f-form-item label='业务员' placeholder='请选择业务员' name='saleManName' @click='openSaleManModal' :readOnly='true' :required='true' :value.sync='formItem.saleManName' :trigger-change='true' v-show='formItem.deptId'>
        <a-tooltip slot='addonBefore' placement='top' title='点击图标选择业务员'>
          <a-icon type='user' style='cursor:pointer' @click='openSaleManModal' />
        </a-tooltip>
      </f-form-item>
      <!--      <f-form-item label='业务区域' type='areaSelect' :value='formItem.areaIds' :required='true' name='areaIds' :level='1' v-if='this.multipleArea==1 || formItem.deptType=="ZS"'></f-form-item>-->

      <f-form-item label='保证金金额' extra='用于产品质押金部分的金额,如收款金额中包含保证金需拆分填写' tooltip='合作保证金' type='number' dataType='number' name='returnAmount' :value='formItem.returnAmount' v-if='formItem.amountType=="CustomBond"' />

      <f-form-item label='汇款方式' type='radio' name='remitType' :required='true' dataType='number' :data='api.busCommon.remitType' :value.sync='formItem.remitType' />
      <f-form-item label='附件' type='upload' name='paymentImage' :value='formItem.paymentImage' :file-max='3' :busId='this.formItem.paymentId' busType='BusPayment' :realRemove='true' @uploadSuccess='uploadSuccess' />
      <f-form-item label='备注' type='textarea' name='remark' :value='formItem.remark' />
    </f-form>
    <select-hy-remoter-custom-modal ref='customModal' @onSelectChange='onSelectCustomChange' :checkDate='false'></select-hy-remoter-custom-modal>
    <select-sale-man-modal ref='saleManModal' @onSelectChange='onSelectSaleManChange' :selectType='2'></select-sale-man-modal>
    <select-dept-modal ref='deptModal' @onSelectChange='onSelectDeptChange'></select-dept-modal>
    <select-bank-modal ref='bankModal' @onSelectChange='onSelectBankChange'></select-bank-modal>
    <select-bank-statement-modal ref='bankStatementModal' @onSelectChange='onSelectBankStatementModalChange'></select-bank-statement-modal>
  </div>
</template>
<script>
import api from "../../js/api"
import { commonMixins } from "@/core/mixins/commonMixins"
import SelectCustomBankModal from "@/views/module/haiying/components/select_custom_bank_modal"
import SelectHyRemoterCustomModal from "@/views/module/haiying/components/select_hy_remoter_custom_modal"
import SelectDeptModal from "@/views/module/haiying/components/select_dept_modal"
import SelectSaleManModal from "@/views/module/haiying/components/select_saleman_modal"
import SelectBankStatementModal from "@/views/module/haiying/components/select_bank_statement_modal"

export default {
  name: "AmountForm",
  props: {},
  mixins: [commonMixins],
  data() {
    return {
      refs: this.$refs,
      api: Object.assign(api, this.api),
      isAdd: true,//是否是添加模式
      fileIds: [],//上传文件ID
      removeIds: [],//删除文件
      loaded: false,//是否加载完成
      autoLoad: false,
      formItem: this.getFormItem()
    }
  },
  methods: {
    getFormItem() {
      return {
        areaIds: [],
        remitType: 2,
        fileList: []
      }
    },


    //表单赋值
    setData(data) {
      this.$refs.form.resetFields()
      this.formItem = this.getFormItem()
      this.formItem.areaIds = []
      if (data && data.paymentId) {
        let params = {
          paymentId: data.paymentId
        }
        this.refs["form"].getDetails(params, (data) => {
          this.formItem = data
          this.formItem.amount = data.amount + ""
          this.formItem.areaIds = [this.formItem.businessAreaId]
          this.formItem.oldBankStatementId = data.bankStatementId
          this.loaded = true
        })
        this.isAdd = false
      } else {
        this.loaded = true
      }
    },
    /**
     * 业务员选择回调
     */
    onSelectSaleManChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "saleManName": selectedRows[0].saleManName })
      this.formItem.saleManName = selectedRows[0].saleManName
      this.formItem.saleManId = selectedRowKeys[0]
      this.$refs.businessArea.initData()
      this.$forceUpdate()
    },
    /**
     * 部门选择回调
     */
    onSelectDeptChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "deptName": selectedRows[0].deptName })
      this.formItem.deptName = selectedRows[0].deptName
      this.formItem.deptId = selectedRows[0].deptId
      this.formItem.deptType = selectedRows[0].deptType
      this.formItem.saleManName = null
      this.$refs.form.getForm().setFieldsValue({ "saleManName": null })
      this.$forceUpdate()
    },
    /**
     * 购货方选择回调
     */
    onSelectCustomChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "customName": selectedRows[0].customName })
      this.formItem.customName = selectedRows[0].customName
      this.formItem.customCode = selectedRows[0].customCode
      this.formItem.sendAreaId = selectedRows[0].sendAreaId
      this.formItem.sendArea = selectedRows[0].sendArea
      if (!this.formItem.payUserName) {
        this.formItem.payUserName = selectedRows[0].customName
      }
      this.$forceUpdate()
    },
    /**
     * 银行选择回调
     */
    onSelectBankChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "bankName": selectedRows[0].bankName })
      this.formItem.bankName = selectedRows[0].bankName
      this.formItem.bankId = selectedRowKeys[0]
      this.formItem.bankNo = selectedRows[0].bankNo
      this.$forceUpdate()
    },
    /**
     * 文件上传回调
     */
    uploadSuccess(fileUrl, files, fileIds, removeIds) {
      this.fileIds = fileIds
      this.removeIds.push(...removeIds)
    },
    /**
     * 银行流水选择回调
     */
    onSelectBankStatementModalChange(selectedRowKeys, selectedRows) {
      this.$refs.form.getForm().setFieldsValue({ "amount": selectedRows[0].amount + "" })
      this.formItem.amountDate = selectedRows[0].amountDate
      this.formItem.amount = selectedRows[0].amount + ""
      this.formItem.bankName = selectedRows[0].takeBankName
      this.formItem.bankId = selectedRows[0].takeBankId
      this.formItem.bankNo = selectedRows[0].takeBankNo
      this.formItem.sendArea = selectedRows[0].sendArea
      this.formItem.payBankNo = selectedRows[0].bankNo
      this.formItem.payUserName = selectedRows[0].customName
      this.formItem.bankStatementId = selectedRows[0].bankStatementId
      let customId = selectedRows[0].customId
      if (customId) {
        let customName = selectedRows[0].customName
        this.formItem.customName = customName
        this.formItem.customId = selectedRows[0].customId
        this.formItem.customCode = selectedRows[0].customCode
        this.$refs.form.getForm().setFieldsValue({ "customName": selectedRows[0].customName })
      }
      
      this.$forceUpdate()
    },
    /**
     * 打开银行流水
     */
    openBankStatementModal() {
      this.$refs["bankStatementModal"].open({})
    },
    //提交表单
    handleSubmit(callback,beforeSubmit) {
      if (this.formItem.amountType != "CustomBond") {
        if (!this.formItem.customCode) {
          this.$message.error("请重新选择单位")
          return
        }
        if (!this.formItem.payUserName) {
          this.$message.error("请选择单位名称")
          return
        }
      }
      this.$refs.form.submit({
        customId: this.formItem.customId,
        customCode: this.formItem.customCode,
        removeIds: this.removeIds,
        bankNo: this.formItem.bankNo,
        payBankId: this.formItem.payBankId,
        payBankNo: this.formItem.payBankNo,
        deptId: this.formItem.deptId,
        saleManId: this.formItem.saleManId,
        sendAreaId: this.formItem.sendAreaId,
        sendArea: this.formItem.sendArea,
        payUserName: this.formItem.payUserName,
        deptType: this.formItem.deptType,
        bankStatementId: this.formItem.bankStatementId,
        oldBankStatementId: this.formItem.oldBankStatementId,
        bankName: this.formItem.bankName,
        amountDate: this.formItem.amountDate,
        bankId: this.formItem.bankId
      }, callback,beforeSubmit)

    }
  },
  created() {

  },
  components: { SelectBankStatementModal, SelectCustomBankModal, SelectHyRemoterCustomModal, SelectDeptModal, SelectSaleManModal }
}
</script>

<style scoped lang='less' rel='stylesheet/less'>

</style>
